package com.helmlabs.grouper.core.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class GenericDaoHibernate <T, ID extends java.io.Serializable> extends HibernateDaoSupport {
	protected Log log = LogFactory.getLog(getClass());
	private Class clazz;
	
	public GenericDaoHibernate(Class clazz){
		this.clazz = clazz;
	}

	public T save(T obj){
		return (T)getHibernateTemplate().merge(obj);
	}
	
	public T get(ID id){
		T obj = (T)getHibernateTemplate().get(clazz, id);
		if (obj==null){
			throw new RuntimeException("object not found");
		}
		return obj;
	}
	
	public List<T> getAll(){
		DetachedCriteria criteria = DetachedCriteria.forClass(clazz);
		return (List<T>)getHibernateTemplate().findByCriteria(criteria);
	}
	
	public void remove(T obj){
		getHibernateTemplate().delete(obj);
	}
	
	

}
